package com.mocoop.util;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.webkit.MimeTypeMap;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class FileUtils
{
  public static String[] fileTypes = { "apk", "avi", "bmp", "chm", "dll", "doc", "docx", "dos", "gif", "html", "jpeg", "jpg", "movie", "mp3", "dat", "mp4", "mpe", "mpeg", "mpg", "pdf", "png", "ppt", "pptx", "rar", "txt", "wav", "wma", "wmv", "xls", "xlsx", "xml", "zip" };

  

  public static String getMIMEType(File paramFile)
  {
    String str1 = "";
    String str2 = paramFile.getName();
    String str3 = str2.substring(str2.lastIndexOf(".") + 1, str2.length()).toLowerCase();
    str1 = MimeTypeMap.getSingleton().getMimeTypeFromExtension(str3);
    return str1;
  }

  public static String getMIMEType(String paramString)
  {
    String str1 = "";
    String str2 = paramString.substring(paramString.lastIndexOf(".") + 1, paramString.length()).toLowerCase();
    str1 = MimeTypeMap.getSingleton().getMimeTypeFromExtension(str2);
    return str1;
  }

  public static void openFile(File paramFile, Activity paramActivity)
  {
    Intent localIntent = new Intent();
    localIntent.addFlags(268435456);
    localIntent.setAction("android.intent.action.VIEW");
    String str = getMIMEType(paramFile);
    localIntent.setDataAndType(Uri.fromFile(paramFile), str);
    try
    {
      paramActivity.startActivity(localIntent);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      Toast.makeText(paramActivity, "没有找到打开此文件的应用!", 1).show();
    }
  }

  public static void openFile(Uri paramUri, String paramString, Activity paramActivity)
  {
    Intent localIntent = new Intent();
    localIntent.addFlags(268435456);
    localIntent.setAction("android.intent.action.VIEW");
    localIntent.setDataAndType(paramUri, paramString);
    try
    {
      paramActivity.startActivity(localIntent);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      Toast.makeText(paramActivity, "没有找到打开此文件的应用!", 1).show();
    }
  }

  public static synchronized void saveObjectToFile(Object paramObject, File paramFile)
    throws Exception
  {
    ObjectOutputStream localObjectOutputStream = new ObjectOutputStream(new FileOutputStream(paramFile));
    localObjectOutputStream.writeObject(paramObject);
    localObjectOutputStream.flush();
    localObjectOutputStream.close();
  }

  public static synchronized Object readObjectFromFile(File paramFile)
    throws Exception
  {
    ObjectInputStream localObjectInputStream = new ObjectInputStream(new FileInputStream(paramFile));
    Object localObject = localObjectInputStream.readObject();
    return localObject;
  }

  /**
	 * @param context
	 * @param parenDir 父路径名称
	 * @param isAppDir 是否使用当前应用的外部存储路径
	 * @return
	 */
	public static File getExternalFile(Context context,String parenDir ,boolean isAppDir){
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			File file = null;
			if(isAppDir){
				file = context.getExternalFilesDir(parenDir);
			}else{
				file = Environment.getExternalStoragePublicDirectory(parenDir);
			}
			if(file != null && !file.exists()){
				file.mkdirs();
			}
			return file;
		}else{
			return null;
		}
	}
  
  public static class MyComparator
    implements Comparator<File>
  {
    public int compare(File paramFile1, File paramFile2)
    {
      return paramFile1.getName().compareTo(paramFile2.getName());
    }
  }
}